package com.jd.fruit.model.admin.auth;

import java.util.List;

import com.jd.fruit.common.annotation.Table;
import com.jd.fruit.common.model.BaseModel;
import com.jd.fruit.common.util.StringUtil;
import com.jfinal.kit.StrKit;

/**
 * 角色
 * 
 */
@SuppressWarnings("serial")
@Table("t_role")
public class Role  extends BaseModel<Role> { 
	public static final Role dao = new Role();
	
	
	/**
	 * 保存栏目
	 * @param role
	 */
	public String save(Role role){
		try {
			String currentId=StringUtil.getKey();
			role.set("id",currentId);
			role.save();
			return currentId;
		} catch (Exception e) {
			throw new RuntimeException("角色保存异常！", e);
		}
	}
	/**
	 * 根据adminid获得角色
	 * @param roleid
	 */
	public Role getRole(String adminid) {
		List<Role> result=Role.dao.find("SELECT r.* FROM t_admin_role ar JOIN t_role r ON ar.`role_id`=r.`id` WHERE ar.`admin_id`=?",adminid);
		return result.size()>0?result.get(0):null;
	}
	
	
	@Override
	public void initWhere() {
		if(StrKit.notBlank(this.getStr("id"))){
			appendSql("AND", "id", "=", getStr("id"));
		}
	}
}
